package org.itboys.core.service.mysql.web;

import org.apache.commons.collections.map.HashedMap;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundayReProductOrderMapper;
import org.itboys.core.dao.order.SundayReProductOrderSKUMapper;
import org.itboys.core.dao.store.SundayStoreNumMapper;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mysql.*;
import org.itboys.core.service.mongo.web.SundayStoreService;
import org.itboys.core.util.NumberUtil;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/22
 * @Description：退货
 */
@Service
public class SundayReProductOrderService extends BaseMySqlService<SundayReProductOrder> {
    private static final String ORDER_PREFIX="ReProductOrder";
    @Autowired
    private SundayReProductOrderMapper reProductOrderMapper;
    @Autowired
    private SundayReProductOrderSKUMapper reProductOrderSKUMapper;
    @Autowired
    private SundayStoreService storeService;
    @Autowired
    private SundaySkuService skuService;
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayStoreNumMapper storeNumMapper;
    @Autowired
    private SundayOutStoreOrderService outStoreOrderService;

    public PageResult<SundayReProductOrder> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayReProductOrder> result = super.selectPage(param);
        List<SundayReProductOrder> orders=result.getData();
        List<Long> ids=new ArrayList<>();
        for (SundayReProductOrder order:orders
                ) {
            ids.add(order.getPreStoreId());
            ids.add(order.getCenterStoreId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in", ids);
        List<SundayStore> stores = storeService.list(param);
        for (SundayReProductOrder order:orders
                ) {
            for (SundayStore store:stores
                    ) {
                if(order.getCenterStoreId()!=null&&order.getCenterStoreId().equals(store.getId())){
                    order.setCenterStoreName(store.getName());
                }
                if(order.getPreStoreId().equals(store.getId())){
                    order.setPreStoreName(store.getName());
                }
            }
        }
        result.setData(orders);
        return result;
    }

    /**
     * 根据仓库id生成退货单,生成sku附表数据
     * @param preStoreId
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Long preStoreId){
        SundayReProductOrder reProductOrder=new SundayReProductOrder(NumberUtil.generateNumber(ORDER_PREFIX)
        ,SundayReProductOrder.ORDER_STATE_SAVE,null,preStoreId,null);
        reProductOrderMapper.insert(reProductOrder);
        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("storeId",preStoreId);
        List<SundayStoreNum> storeNums = storeNumMapper.select(param);
        List<SundayReProductOrderSKU> reProductOrderSKUS=new ArrayList<>();
        for (SundayStoreNum storeNum:storeNums
             ) {
            SundayReProductOrderSKU reProductOrderSKU=new SundayReProductOrderSKU();
            reProductOrderSKU.setOrderId(reProductOrder.getId());
            reProductOrderSKU.setSKUId(storeNum.getSKUId());
            reProductOrderSKU.setNum(storeNum.getNum());
            reProductOrderSKUS.add(reProductOrderSKU);
        }
        reProductOrderSKUMapper.insertSKUs(reProductOrderSKUS);
    }

    /**
     * 根据退货单id获取详情
     * @param reProductOrderId
     * @return
     */
    public SundayReProductOrder findOne(Long reProductOrderId){
        if(!LongUtil.isNotZreo(reProductOrderId)){
            return null;
        }
        SundayReProductOrder order = reProductOrderMapper.find(reProductOrderId);
        List<Long> ids=new ArrayList<>();
        ids.add(order.getCenterStoreId());
        ids.add(order.getPreStoreId());
        Map<String,Object> param=new HashMap();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",ids);
        List<SundayStore> stores = storeService.list(param);
        for (SundayStore store:stores
                ) {
            if(order.getCenterStoreId()!=null&&order.getCenterStoreId().equals(store.getId())){
                order.setCenterStoreName(store.getName());
            }
            if(order.getPreStoreId().equals(store.getId())){
                order.setPreStoreName(store.getName());
            }
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("orderId",order.getId());
        List<SundayReProductOrderSKU> skuOrders = reProductOrderSKUMapper.select(param);
        ids.clear();
        for (SundayReProductOrderSKU skuOrder:skuOrders
                ) {
            ids.add(skuOrder.getSKUId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundaySku> skus = skuService.select(param);
        ids.clear();
        for (SundaySku sku:skus
                ) {
            ids.add(sku.getProductId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("ids",ids);
        List<SundayProduct> products = productService.select(param);
        for (SundayReProductOrderSKU skuOrder:skuOrders) {
            for (SundaySku sku:skus
                    ) {
                if(skuOrder.getSKUId().equals(sku.getId())){
                    for (SundayProduct product :products) {
                        if(product.getId().equals(sku.getProductId())){
                            skuOrder.setProductName(product.getName()+"("+sku.getNumber()+")");
                            break;
                        }
                    }
                    break;
                }
            }
        }
        order.setSkus(skuOrders);
        return order;
    }

    public void setState(Long reProductOrderId,Integer state){
        SundayReProductOrder order =new SundayReProductOrder();
        order.setId(reProductOrderId);
        order.setState(state);
        reProductOrderMapper.update(order);
        if(SundayReProductOrder.ORDER_STATE_THROUGH.equals(state)){
            //审核通过同时生成出库单
            SundayReProductOrder reProductOrder = super.find(reProductOrderId);
            Map<String,Object> param=new HashMap<>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("orderId",reProductOrderId);
            List<SundayReProductOrderSKU> skus = reProductOrderSKUMapper.select(param);
            outStoreOrderService.save(reProductOrder,skus);
        }
    }
}

